package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HoChangeApplication;
import com.ruoyi.system.domain.HoOccupancyInfo;
import com.ruoyi.system.service.IHoOccupancyInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HoCheckoutApplication;
import com.ruoyi.system.service.IHoCheckoutApplicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 退宿申请Controller
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/system/checkoutApplication")
public class HoCheckoutApplicationController extends BaseController {
    @Autowired
    private IHoCheckoutApplicationService hoCheckoutApplicationService;

    @Autowired
    private IHoOccupancyInfoService hoOccupancyInfoService;

    /**
     * 查询退宿申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:checkoutApplication:list')")
    @GetMapping("/list")
    public TableDataInfo list(HoCheckoutApplication hoCheckoutApplication) {
        startPage();
        if (null != hoCheckoutApplication.getStudentId() && hoCheckoutApplication.getStudentId() == 0) {
            hoCheckoutApplication.setStudentId(null);
        } else {
            hoCheckoutApplication.setStudentId(getUserId());
        }
        List<HoCheckoutApplication> list = hoCheckoutApplicationService.selectHoCheckoutApplicationList(hoCheckoutApplication);
        return getDataTable(list);
    }

    /**
     * 导出退宿申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:checkoutApplication:export')")
    @Log(title = "退宿申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoCheckoutApplication hoCheckoutApplication) {
        if (null != hoCheckoutApplication.getStudentId() && hoCheckoutApplication.getStudentId() == 0) {
            hoCheckoutApplication.setStudentId(null);
        } else {
            hoCheckoutApplication.setStudentId(getUserId());
        }
        List<HoCheckoutApplication> list = hoCheckoutApplicationService.selectHoCheckoutApplicationList(hoCheckoutApplication);
        ExcelUtil<HoCheckoutApplication> util = new ExcelUtil<HoCheckoutApplication>(HoCheckoutApplication.class);
        util.exportExcel(response, list, "退宿申请数据");
    }

    /**
     * 获取退宿申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:checkoutApplication:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hoCheckoutApplicationService.selectHoCheckoutApplicationById(id));
    }

    /**
     * 新增退宿申请
     */
    @PreAuthorize("@ss.hasPermi('system:checkoutApplication:add')")
    @Log(title = "退宿申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HoCheckoutApplication hoCheckoutApplication) {
        HoOccupancyInfo param = new HoOccupancyInfo();
        param.setStudentNum(getLoginUser().getUser().getUserName());
        param.setStatus("1");
        List<HoOccupancyInfo> hoOccupancyInfos = hoOccupancyInfoService.selectHoOccupancyInfoList(param);
        if (CollectionUtils.isEmpty(hoOccupancyInfos)) {
            throw new RuntimeException(getLoginUser().getUser().getNickName() + "|" + getLoginUser().getUser().getUserName() + " 当前状态不是在宿状态，不能进行退宿申请!");
        }
        hoCheckoutApplication.setStudentId(getUserId());
        hoCheckoutApplication.setName(getLoginUser().getUser().getNickName());
        hoCheckoutApplication.setStudentNum(getLoginUser().getUser().getUserName());
        hoCheckoutApplication.setApplicationTime(DateUtils.getNowDate());
        hoCheckoutApplication.setReviewStatus("2");
        hoCheckoutApplication.setCreateUserId(getUserId());
        return toAjax(hoCheckoutApplicationService.insertHoCheckoutApplication(hoCheckoutApplication));
    }

    /**
     * 修改退宿申请
     */
    @PreAuthorize("@ss.hasPermi('system:checkoutApplication:edit')")
    @Log(title = "退宿申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HoCheckoutApplication hoCheckoutApplication) {
        HoCheckoutApplication old = hoCheckoutApplicationService.selectHoCheckoutApplicationById(hoCheckoutApplication.getId());
        if (!"2".equals(old.getReviewStatus())) {
            throw new RuntimeException("不是未审核状态的不能进行操作！");
        }
        if ("3".equals(hoCheckoutApplication.getReviewStatus())) {
            // 撤回
        } else if ("1".equals(hoCheckoutApplication.getReviewStatus())) {
            // 审核通过
            HoOccupancyInfo param = new HoOccupancyInfo();
            param.setStudentNum(old.getStudentNum());
            param.setStatus("1");
            List<HoOccupancyInfo> hoOccupancyInfos = hoOccupancyInfoService.selectHoOccupancyInfoList(param);
            if (CollectionUtils.isEmpty(hoOccupancyInfos)) {
                throw new RuntimeException(old.getName() + "|" + old.getStudentNum() + " 当前状态不是在宿状态，不能退宿房间!");
            }
            HoOccupancyInfo hoOccupancyInfo = hoOccupancyInfos.get(0);
            hoOccupancyInfoService.checkoutApplication(hoOccupancyInfo, getUserId(), old.getApplicationInfo(), old.getRequestedTime());
        } else if ("0".equals(hoCheckoutApplication.getReviewStatus())) {
            // 审核拒绝
        }
        return toAjax(hoCheckoutApplicationService.updateHoCheckoutApplication(hoCheckoutApplication));
    }

    /**
     * 删除退宿申请
     */
    @PreAuthorize("@ss.hasPermi('system:checkoutApplication:remove')")
    @Log(title = "退宿申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hoCheckoutApplicationService.deleteHoCheckoutApplicationByIds(ids));
    }
}
